package buffer;

import java.nio.ByteBuffer;

import storage.storageMgr;


 public class Buf{
	 int bid;
	 int pid = -1;
	 String dbName = "noname";
	 int lruNext, lruPrev, haNext;
	 ByteBuffer data;
	 int pinCnt = 0;
	 boolean dirty = false;
	
	private storageMgr comsMgr;
	
	Buf(storageMgr s, int id){
		comsMgr = s;
		bid = id;
	}
	
	public String toString(){
		return "(" + dbName +" , " + pid + ")";
		}

	 boolean check(String name, int id) { return ( name == dbName && ( id == 0 || pid == id )); }
	 boolean isDirty() {return dirty;}
	 boolean isFree() {return (pinCnt == 0);}
	 
	 /**
	  * data from disk -> buffer
	  */
	 void disk2buf(){
		data = ByteBuffer.allocate(comsMgr.pageSize());
		comsMgr.readPage(dbName, pid, data);
	}
	 
	 /**
	  * data from buffer -> disk
	  */
	 void buf2disk(){
		comsMgr.writePage(dbName, pid, data);
		dirty = false;
	}
	
	 /**
	  * data from higher level -> buffer 
	  */
	 void hi2buf(ByteBuffer bb){
		data = bb.duplicate();
		dirty = true;
	}
	
	 /**
	  * data from buffer -> higher level  
	  */
	 ByteBuffer buf2hi(){
		return data;
	}


}
